﻿using log4net;
using SqlSugar;
using System;
using System.Linq;

namespace Mate.Db.SqlSugar
{
    public class DataClient : SqlSugarClient
    {
        public DataClient(DataSettings dataSettings, ILog log) : base(new ConnectionConfig()
        {
            ConnectionString = dataSettings.DetaultConnectionString,
            IsAutoCloseConnection = dataSettings.IsAutoCloseConnection,
            DbType = dataSettings.GetDbType()
        })
        {
            if (dataSettings.IsEnableLogEvent == true)
            {
                Ado.IsEnableLogEvent = true;
                Ado.LogEventStarting = (sql, pars) =>
                {
                    log.Info(sql + "\r\n" + Utilities.SerializeObject(pars.ToDictionary(s => s.ParameterName, s => s.Value)));
                };
            }
        }
    }
}
